﻿using Abp.Application.Services.Dto;
using Abp.Application.Services;
using Abp.Domain.Repositories;
using System;
using Choucius.Boilerplate9.ProductContainerRelationships.Dto;
using System.Linq;

namespace Choucius.Boilerplate9.ProductContainerRelationships
{
    /// <summary>
    /// 产品与容器的绑定关系
    /// </summary>
    public class ProductContainerRelationshipAppService : AsyncCrudAppService<ProductContainerRelationship, ProductContainerRelationshipDto, Guid, PagedResultRequestDto, ProductContainerRelationshipDto, ProductContainerRelationshipDto>, IProductContainerRelationshipAppService
    {
        private readonly IRepository<ProductContainerRelationship, Guid> _repository;

        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="repository"></param>
        public ProductContainerRelationshipAppService(IRepository<ProductContainerRelationship, Guid> repository) : base(repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 根据吸塑盒ID获取该吸塑盒可以摆放的产品数量
        /// </summary>
        /// <param name="productErpId">吸塑盒ID</param>
        /// <returns>该吸塑盒可以摆放的产品数量</returns>
        public int GetQuantityPerPlasticContainerByProduct(string productErpId)
        {
            var c = _repository.GetAll().ToList();
            var entity = _repository.FirstOrDefault(x => x.ProductErpId == productErpId);
            if (entity == null)
            {
                return 0;
            }
            //var b = _repository.Single(x => x.ProductErpId == productErpId);
            int quantity = entity.NumberOfLayers;
            return quantity;
        }

        /// <summary>
        /// 根据吸塑盒ERP_ID获取吸塑盒名称
        /// </summary>
        /// <param name="plasticContainerErpId"></param>
        /// <returns></returns>
        public string GetPlasticContainerNameByPlasticContainerErpId(string plasticContainerErpId)
        {
            var entity = _repository.FirstOrDefault(x => x.PlasticContainerErpId == plasticContainerErpId);
            if ( entity == null )
            {
                return string.Empty;
            }
            return entity.PlasticContainerName;
        }
    }
}
